using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;
namespace Medianamik.Core.DAL.SubSonicDAL
{
	/// <summary>
	/// Strongly-typed collection for the InterfaceX class.
	/// </summary>
    [Serializable]
	public partial class InterfaceXCollection : ActiveList<InterfaceX, InterfaceXCollection>
	{	   
		public InterfaceXCollection() {}
        
        /// <summary>
		/// Filters an existing collection based on the set criteria. This is an in-memory filter
		/// Thanks to developingchris for this!
        /// </summary>
        /// <returns>InterfaceXCollection</returns>
		public InterfaceXCollection Filter()
        {
            for (int i = this.Count - 1; i > -1; i--)
            {
                InterfaceX o = this[i];
                foreach (SubSonic.Where w in this.wheres)
                {
                    bool remove = false;
                    System.Reflection.PropertyInfo pi = o.GetType().GetProperty(w.ColumnName);
                    if (pi.CanRead)
                    {
                        object val = pi.GetValue(o, null);
                        switch (w.Comparison)
                        {
                            case SubSonic.Comparison.Equals:
                                if (!val.Equals(w.ParameterValue))
                                {
                                    remove = true;
                                }
                                break;
                        }
                    }
                    if (remove)
                    {
                        this.Remove(o);
                        break;
                    }
                }
            }
            return this;
        }
		
		
	}
	/// <summary>
	/// This is an ActiveRecord class which wraps the medianamik_Interface table.
	/// </summary>
	[Serializable]
	public partial class InterfaceX : ActiveRecord<InterfaceX>, IActiveRecord
	{
		#region .ctors and Default Settings
		
		public InterfaceX()
		{
		  SetSQLProps();
		  InitSetDefaults();
		  MarkNew();
		}
		
		private void InitSetDefaults() { SetDefaults(); }
		
		public InterfaceX(bool useDatabaseDefaults)
		{
			SetSQLProps();
			if(useDatabaseDefaults)
				ForceDefaults();
			MarkNew();
		}
        
		public InterfaceX(object keyID)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByKey(keyID);
		}
		 
		public InterfaceX(string columnName, object columnValue)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByParam(columnName,columnValue);
		}
		
		protected static void SetSQLProps() { GetTableSchema(); }
		
		#endregion
		
		#region Schema and Query Accessor	
		public static Query CreateQuery() { return new Query(Schema); }
		public static TableSchema.Table Schema
		{
			get
			{
				if (BaseSchema == null)
					SetSQLProps();
				return BaseSchema;
			}
		}
		
		private static void GetTableSchema() 
		{
			if(!IsSchemaInitialized)
			{
				//Schema declaration
				TableSchema.Table schema = new TableSchema.Table("medianamik_Interface", TableType.Table, DataService.GetInstance("MedianamikProvider"));
				schema.Columns = new TableSchema.TableColumnCollection();
				schema.SchemaName = @"dbo";
				//columns
				
				TableSchema.TableColumn colvarInterfaceId = new TableSchema.TableColumn(schema);
				colvarInterfaceId.ColumnName = "InterfaceId";
				colvarInterfaceId.DataType = DbType.Guid;
				colvarInterfaceId.MaxLength = 0;
				colvarInterfaceId.AutoIncrement = false;
				colvarInterfaceId.IsNullable = false;
				colvarInterfaceId.IsPrimaryKey = true;
				colvarInterfaceId.IsForeignKey = false;
				colvarInterfaceId.IsReadOnly = false;
				colvarInterfaceId.DefaultSetting = @"";
				colvarInterfaceId.ForeignKeyTableName = "";
				schema.Columns.Add(colvarInterfaceId);
				
				TableSchema.TableColumn colvarName = new TableSchema.TableColumn(schema);
				colvarName.ColumnName = "Name";
				colvarName.DataType = DbType.AnsiString;
				colvarName.MaxLength = 50;
				colvarName.AutoIncrement = false;
				colvarName.IsNullable = false;
				colvarName.IsPrimaryKey = false;
				colvarName.IsForeignKey = false;
				colvarName.IsReadOnly = false;
				colvarName.DefaultSetting = @"";
				colvarName.ForeignKeyTableName = "";
				schema.Columns.Add(colvarName);
				
				TableSchema.TableColumn colvarDescription = new TableSchema.TableColumn(schema);
				colvarDescription.ColumnName = "Description";
				colvarDescription.DataType = DbType.AnsiString;
				colvarDescription.MaxLength = -1;
				colvarDescription.AutoIncrement = false;
				colvarDescription.IsNullable = true;
				colvarDescription.IsPrimaryKey = false;
				colvarDescription.IsForeignKey = false;
				colvarDescription.IsReadOnly = false;
				colvarDescription.DefaultSetting = @"";
				colvarDescription.ForeignKeyTableName = "";
				schema.Columns.Add(colvarDescription);
				
				BaseSchema = schema;
				//add this schema to the provider
				//so we can query it later
				DataService.Providers["MedianamikProvider"].AddSchema("medianamik_Interface",schema);
			}
		}
		#endregion
		
		#region Props
		  
		[XmlAttribute("InterfaceId")]
		[Bindable(true)]
		public Guid InterfaceId 
		{
			get { return GetColumnValue<Guid>(Columns.InterfaceId); }
			set { SetColumnValue(Columns.InterfaceId, value); }
		}
		  
		[XmlAttribute("Name")]
		[Bindable(true)]
		public string Name 
		{
			get { return GetColumnValue<string>(Columns.Name); }
			set { SetColumnValue(Columns.Name, value); }
		}
		  
		[XmlAttribute("Description")]
		[Bindable(true)]
		public string Description 
		{
			get { return GetColumnValue<string>(Columns.Description); }
			set { SetColumnValue(Columns.Description, value); }
		}
		
		#endregion
		
		
		#region PrimaryKey Methods		
		
        protected override void SetPrimaryKey(object oValue)
        {
            base.SetPrimaryKey(oValue);
            
            SetPKValues();
        }
        
		
		private Medianamik.Core.DAL.SubSonicDAL.InterfacePropertyCollection colInterfacePropertyRecords;
		public Medianamik.Core.DAL.SubSonicDAL.InterfacePropertyCollection InterfacePropertyRecords()
		{
			if(colInterfacePropertyRecords == null)
			{
				colInterfacePropertyRecords = new Medianamik.Core.DAL.SubSonicDAL.InterfacePropertyCollection().Where(InterfaceProperty.Columns.InterfaceId, InterfaceId).Load();
				colInterfacePropertyRecords.ListChanged += new ListChangedEventHandler(colInterfacePropertyRecords_ListChanged);
			}
			return colInterfacePropertyRecords;
		}
				
		void colInterfacePropertyRecords_ListChanged(object sender, ListChangedEventArgs e)
		{
            if (e.ListChangedType == ListChangedType.ItemAdded)
            {
		        // Set foreign key value
		        colInterfacePropertyRecords[e.NewIndex].InterfaceId = InterfaceId;
				colInterfacePropertyRecords.ListChanged += new ListChangedEventHandler(colInterfacePropertyRecords_ListChanged);
            }
		}
		private Medianamik.Core.DAL.SubSonicDAL.TypeInterfaceCollection colTypeInterfaceRecords;
		public Medianamik.Core.DAL.SubSonicDAL.TypeInterfaceCollection TypeInterfaceRecords()
		{
			if(colTypeInterfaceRecords == null)
			{
				colTypeInterfaceRecords = new Medianamik.Core.DAL.SubSonicDAL.TypeInterfaceCollection().Where(TypeInterface.Columns.InterfaceId, InterfaceId).Load();
				colTypeInterfaceRecords.ListChanged += new ListChangedEventHandler(colTypeInterfaceRecords_ListChanged);
			}
			return colTypeInterfaceRecords;
		}
				
		void colTypeInterfaceRecords_ListChanged(object sender, ListChangedEventArgs e)
		{
            if (e.ListChangedType == ListChangedType.ItemAdded)
            {
		        // Set foreign key value
		        colTypeInterfaceRecords[e.NewIndex].InterfaceId = InterfaceId;
				colTypeInterfaceRecords.ListChanged += new ListChangedEventHandler(colTypeInterfaceRecords_ListChanged);
            }
		}
		#endregion
		
			
		
		//no foreign key tables defined (0)
		
		
		
		//no ManyToMany tables defined (0)
		
        
        
		#region ObjectDataSource support
		
		
		/// <summary>
		/// Inserts a record, can be used with the Object Data Source
		/// </summary>
		public static void Insert(Guid varInterfaceId,string varName,string varDescription)
		{
			InterfaceX item = new InterfaceX();
			
			item.InterfaceId = varInterfaceId;
			
			item.Name = varName;
			
			item.Description = varDescription;
			
		
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}
		
		/// <summary>
		/// Updates a record, can be used with the Object Data Source
		/// </summary>
		public static void Update(Guid varInterfaceId,string varName,string varDescription)
		{
			InterfaceX item = new InterfaceX();
			
				item.InterfaceId = varInterfaceId;
			
				item.Name = varName;
			
				item.Description = varDescription;
			
			item.IsNew = false;
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}
		#endregion
        
        
        
        #region Typed Columns
        
        
        public static TableSchema.TableColumn InterfaceIdColumn
        {
            get { return Schema.Columns[0]; }
        }
        
        
        
        public static TableSchema.TableColumn NameColumn
        {
            get { return Schema.Columns[1]; }
        }
        
        
        
        public static TableSchema.TableColumn DescriptionColumn
        {
            get { return Schema.Columns[2]; }
        }
        
        
        
        #endregion
		#region Columns Struct
		public struct Columns
		{
			 public static string InterfaceId = @"InterfaceId";
			 public static string Name = @"Name";
			 public static string Description = @"Description";
						
		}
		#endregion
		
		#region Update PK Collections
		
        public void SetPKValues()
        {
                if (colInterfacePropertyRecords != null)
                {
                    foreach (Medianamik.Core.DAL.SubSonicDAL.InterfaceProperty item in colInterfacePropertyRecords)
                    {
                        if (item.InterfaceId != InterfaceId)
                        {
                            item.InterfaceId = InterfaceId;
                        }
                    }
               }
		
                if (colTypeInterfaceRecords != null)
                {
                    foreach (Medianamik.Core.DAL.SubSonicDAL.TypeInterface item in colTypeInterfaceRecords)
                    {
                        if (item.InterfaceId != InterfaceId)
                        {
                            item.InterfaceId = InterfaceId;
                        }
                    }
               }
		}
        #endregion
    
        #region Deep Save
		
        public void DeepSave()
        {
            Save();
            
                if (colInterfacePropertyRecords != null)
                {
                    colInterfacePropertyRecords.SaveAll();
               }
		
                if (colTypeInterfaceRecords != null)
                {
                    colTypeInterfaceRecords.SaveAll();
               }
		}
        #endregion
	}
}
